// pages/homehall/homehall.js
var {
  Api
} = require('../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flage: true,//选择和底部的是否出来
    list: '',//题库分类列表
    listindex: 1,//题库分类列表索引
    page: 1,
    page_size: 100,
    items: '',//列表
    type: 1,//1是删除我的题库2是加入我的题库
    xuannum: [],//选中个数的id
    check: false,//是否全部选中
    kong: false,//items是否为空
    vip:false,//是否为vip
    domain: Api.domain,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      listindex: options.index,
    })
    this.tilist()
  },
  binddian() {
    if (this.data.kong) {
      wx.showToast({
        title: '暂无题库可操作',
        icon: 'none'
      })
      return
    }
    this.setData({
      flage: !this.data.flage
    })
  },
  tilist() {//题库分类
    wx.$http.get(wx.$get.library_class, {}).then(res => {
      let ress = [{ id: '', library_class_name: '我的题库' }]
      this.setData({
        list: [...ress, ...res]
      })
      console.log(this.data.list, '题库分类')
      if (this.data.listindex == 0) {
        this.myti()
      } else {
        this.tidetail()
      }
    })
  },
  tidetail() {//题库列表
    let params = {
      member_id: wx.$cache.get('member_id'),
      class_id: this.data.list[this.data.listindex].id,
      page: this.data.page,
      page_size: this.data.page_size
    }
    wx.$http.get(wx.$get.library_list, params).then(res => {
      console.log(res, '题库列表')
      if (res.length == 0) {
        this.setData({
          kong: true
        })
      } else {
        this.setData({
          kong: false
        })
      }
      this.setData({
        items: res
      })
    })
  },
  qie(e) {//分类列表切换
    let { index } = e.currentTarget.dataset
    this.setData({
      listindex: index,
      flage: true,
      items: [],
      xuannum: []
    })
    if (this.data.listindex == 0) {
      this.setData({
        type: 1
      })
      this.myti()
    } else {
      this.setData({
        type: 2
      })
      this.tidetail()
    }
  },
  myti() {//我的题库
    let params = {
      page: this.data.page,
      page_size: this.data.page_size,
    }
    wx.$http.get(wx.$get.my_library, params).then(res => {
      console.log(res, '我的题库')
      if (res.length == 0) {
        this.setData({
          kong: true
        })
      } else {
        this.setData({
          kong: false
        })
      }
      this.setData({
        items: res
      })
    })
  },
  bindxuan(e) {//选择点
    let { index } = e.currentTarget.dataset
    this.setData({
      [`items[${index}].flage`]: !this.data.items[index].flage
    })
    let check = this.data.items.every(item => item.flage == true)//是否全部选中
    this.setData({
      check
    })
    let xuannum = [];//选中的个数的id
    for (let i = 0; i < this.data.items.length; i++) {
      if (this.data.items[i].flage) {
        xuannum = xuannum.concat(this.data.items[i].id)
      }
    }
    this.setData({
      xuannum
    })
  },
  bindquan() {//全选
    let { items } = this.data
    if (this.data.check) {
      for (let i = 0; i < items.length; i++) {
        items[i].flage = false
      }
    } else {
      for (let i = 0; i < items.length; i++) {
        items[i].flage = true
      }
    }
    let xuannum = [];//选中的个数的id
    for (let i = 0; i < items.length; i++) {
      if (items[i].flage) {
        xuannum = xuannum.concat(items[i].id)
      }
    }
    this.setData({
      check: !this.data.check,
      items,
      xuannum
    })
  },
  addti() {//批量加入题库
    if (this.data.xuannum.length == 0) {
      wx.showToast({
        title: '请先勾选题库！',
        icon: 'none'
      })
      return
    }
    let viptime=new Date(wx.$cache.get('userinfo').vip_time.replace(/\-/g, '/')).getTime();
    let today=new Date().getTime()
    let viptrue=false
    if(viptime>today){
      viptrue=true
    }
  
      wx.showModal({
        title: '提示',
        content: '你确定要添加吗？',
        success: (res) => {
          if (res.confirm) {
            let params = {
              library_ids: this.data.xuannum.toString()
            }
            wx.$http.get(wx.$get.add_all_library, params).then(res => {
              wx.showToast({
                title: '添加成功',
              })
              this.itemsqing()
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
   
   
  },
  itemsqing() {//清空选中的状态
    let { items } = this.data
    for (let i = 0; i < items.length; i++) {
      items[i].flage = false
    }
    this.setData({
      items,
      check: false,
      xuannum: [],
      flage: true
    })
  },
  delti() {//批量删除题库
    if (this.data.xuannum.length == 0) {
      wx.showToast({
        title: '请先勾选题库！',
        icon: 'none'
      })
      return
    }
    wx.showModal({
      title: '提示',
      content: '你确定要移除吗？',
      success: (res) => {
        if (res.confirm) {
          let params = {
            library_ids: this.data.xuannum.toString()
          }
          wx.$http.get(wx.$get.del_all_library, params).then(res => {
            wx.showToast({
              title: '移除成功',
            })
            this.setData({
              check: false,
              xuannum: [],
              flage: true
            })
            this.myti()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  godetail(e) {//返回上个页面，并显示当前题库
    let { index } = e.currentTarget.dataset
    // if (this.data.listindex == 0) {
    //   var pageArr = getCurrentPages();
    //   var thisPage = pageArr[pageArr.length - 2];
    //   thisPage.setData({
    //     blackid: this.data.items[index].id
    //   })
    //   wx.navigateBack({
    //     delta: 1,
    //   })
    // } else {
      if(this.data.vip){
        this.addoneti(index)
      }else{
        if(this.data.items[index].is_vip==1){
          wx.navigateTo({
            url: '/pages/memberCenter/memberCenter',
          })
        }else{
          this.addoneti(index)
        }
      // }
    }
  },
  addoneti(e) {//添加一个题库
    let index = e
    let params = {
      library_id: this.data.items[index].id
    }
    wx.$http.get(wx.$get.add_my_library, params).then(res => {
      console.log(res, '添加一个题库')
      var pageArr = getCurrentPages();
      var thisPage = pageArr[pageArr.length - 2];
      thisPage.setData({
        blackid: this.data.items[index].id
      })
      wx.navigateBack({
        delta: 1,
      })
    }).catch(err => {
      var pageArr = getCurrentPages();
      var thisPage = pageArr[pageArr.length - 2];
      thisPage.setData({
        blackid: this.data.items[index].id
      })
      wx.navigateBack({
        delta: 1,
      })
    })
  },
  judgeTime(time) { //判断时间是否过期
    if (!time) {
      return false
    }
    var strtime = time.replace(/-/g, "/");//时间转换
    //时间
    var date1 = Date.parse(new Date(strtime));
    //现在时间
    var date2 = Date.parse(new Date());
    //判断时间是否过期
    return date1 < date2 ? false : true;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      vip:this.judgeTime(wx.$cache.get('userinfo').vip_time)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:wx.$cache.get('fx').title,
      imageUrl:wx.$cache.get('fx').img,
      path:`/pages/index/index?pid=${wx.$cache.get('member_id')}`
    }
  }
})